package wangwenjun.phase1.interrupt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.ThreadUtil;

/**
 * @author ChangLiang
 * @date 2020/5/29
 */
public class ThreadIsInterruptedV3 {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadIsInterruptedV3.class);

    public static void main(String[] args) {
        // 由于在run方法中使用了sleep 这个可中断方法，它会捕获到中断信号，并且会擦除interrupt 标识，因此程序的执行结果都会是false
        Thread thread = new Thread() {
            @Override
            public void run() {
                while (true) {
                    LOGGER.info("no sleep: I am be interrupted ? {}", isInterrupted());
                }
            }
        };

        thread.setDaemon(true);
        thread.start();
        ThreadUtil.sleepForMilliSeconds(2);
        LOGGER.info("Thread is interrupted ? {}", thread.isInterrupted());
        thread.interrupt();
        ThreadUtil.sleepForMilliSeconds(2);
        LOGGER.info("Thread is interrupted ? {}", thread.isInterrupted());

        // output: false false false
    }
}
